package msu.erdc.hci;

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import msu.erdc.hci.DateDialogFragment.DateDialogFragmentListener;
import msu.erdc.hci.customObjects.InsuranceObject;
import msu.erdc.hci.customObjects.VehicleObject;

import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

public class Vehicle extends Fragment {
	
	EditText name;
	EditText tagNum;
	EditText vinNum;
	Spinner yearSpinner;
	EditText color;
	EditText registrationNum;
	Button mediaButton;
	Button tagDateButton;
	Button inspectionDateButton;
	Button insuranceButton;
	Spinner makeSpinner;
	Spinner modelSpinner;
	VehicleObject vehicle;
	ListAdapter insuranceAdapter;
	List<String> yearList;
	List<String> makeList;
	List<String> dodgeModels = Arrays.asList(new String[]{"Ram 1500", "Charger", "Challenger"});
	List<String> fordModels = Arrays.asList(new String[]{"F-150", "Ranger", "Escape"});
	List<String> hondaModels = Arrays.asList(new String[]{"Accord", "CRV", "Pilot"});
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View view =  inflater.inflate(R.layout.vehicle, container, false);
		//do ui stuff here
		final FragmentManager manager = getFragmentManager();
		
		name = (EditText) view.findViewById(R.id.nameText);
		name.setText(vehicle.name);
		
		tagNum = (EditText) view.findViewById(R.id.tagText);
		tagNum.setText(vehicle.tagNum);
		
		vinNum = (EditText) view.findViewById(R.id.vinText);
		vinNum.setText(vehicle.vin);
		
		yearSpinner = (Spinner) view.findViewById(R.id.year);
		int yearIndex = yearList.indexOf(String.valueOf(vehicle.year));
		if(yearIndex > -1)
			yearSpinner.setSelection(yearIndex);
		
		color = (EditText) view.findViewById(R.id.colorText);
		color.setText(vehicle.color);
		
		registrationNum = (EditText) view.findViewById(R.id.registrationText);
		registrationNum.setText(vehicle.registrationNum);
		
		mediaButton = (Button) view.findViewById(R.id.mediaButton);
		mediaButton.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				FragmentTransaction ft = manager.beginTransaction();
				Media media = new Media();
				Bundle arguments = new Bundle();
				arguments.putString("breadcrumbText", "Vehicles/" + name.getEditableText().toString());
				media.setArguments(arguments);
				ft.replace(android.R.id.content, media, "media");
				ft.addToBackStack(null);
				ft.commit();
			}});
		
		tagDateButton = (Button) view.findViewById(R.id.tagDateButton);
		if(vehicle.tagMonth!=0 && vehicle.tagYear!=0)
			tagDateButton.setText(vehicle.tagMonth + " / " + vehicle.tagYear);
		tagDateButton.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				Calendar tagDate = Calendar.getInstance();
				int month = vehicle.tagMonth;
				tagDate.set(vehicle.tagYear, month==0?month:month-1, 1);
				DateDialogFragment ddf = DateDialogFragment.newInstance(getActivity(), R.string.tag_date_title, tagDate, false);
				ddf.setDateDialogFragmentListener(new DateDialogFragmentListener() {
				 
				    public void dateDialogFragmentDateSet(Calendar date) {
				        // update the fragment
				    	tagDateButton.setText(date.get(Calendar.MONTH)+1 + " / " + date.get(Calendar.YEAR));
				    	vehicle.tagMonth = date.get(Calendar.MONTH)+1;
				    	vehicle.tagYear = date.get(Calendar.YEAR);
				    }
				});
				 
				ddf.show(getFragmentManager(), "date picker dialog fragment");
			}});
		
		inspectionDateButton = (Button) view.findViewById(R.id.inspectionDateButton);
		if(vehicle.inspectionMonth!=0 && vehicle.inspectionYear!=0)
			inspectionDateButton.setText(vehicle.inspectionMonth + " / " + vehicle.inspectionYear);
		inspectionDateButton.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				Calendar inspectionDate = Calendar.getInstance();
				int month = vehicle.inspectionMonth;
				inspectionDate.set(vehicle.inspectionYear, month==0?month:month-1, 1);
				DateDialogFragment ddf = DateDialogFragment.newInstance(getActivity(), R.string.inspection_date_title, inspectionDate, false);
				ddf.setDateDialogFragmentListener(new DateDialogFragmentListener() {
				 
				    public void dateDialogFragmentDateSet(Calendar date) {
				        // update the fragment
				    	inspectionDateButton.setText(date.get(Calendar.MONTH)+1 + " / " + date.get(Calendar.YEAR));
				    	vehicle.inspectionMonth = date.get(Calendar.MONTH)+1;
				    	vehicle.inspectionYear = date.get(Calendar.YEAR);
				    }
				});
				 
				ddf.show(getFragmentManager(), "date picker dialog fragment");
			}});
		
		insuranceButton = (Button) view.findViewById(R.id.insuranceButton);
		if(vehicle.selectedInsurancePolicy!=null && !vehicle.selectedInsurancePolicy.equals(""))
			insuranceButton.setText(vehicle.selectedInsurancePolicy);
		insuranceButton.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				new AlertDialog.Builder(getActivity())
				.setAdapter(insuranceAdapter, new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						vehicle.selectedInsurancePolicy = ((Global)getActivity().getApplication()).insurances.get(which).toString();
						insuranceButton.setText(vehicle.selectedInsurancePolicy);
					}
				})
				.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						//do nothing
					}
				})
				.setTitle("Select this vehicle's Insurance")
				.setIcon(android.R.drawable.ic_dialog_info)
				.show();
			}});
		
		makeSpinner = (Spinner) view.findViewById(R.id.make);
		int makeIndex = makeList.indexOf(vehicle.make);
		if(makeIndex > -1)
			makeSpinner.setSelection(makeIndex);
		makeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				updateModelSpinner();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				//do nothing
			}
		});
		modelSpinner = (Spinner) view.findViewById(R.id.model);
		
        return view;
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //TODO
        //if not passed in
        Bundle arguments = getArguments();
        if(!arguments.containsKey("vehicleIndex"))
        	vehicle = new VehicleObject();
        else
        	vehicle = ((Global)getActivity().getApplication()).vehicles.get(arguments.getInt("vehicleIndex"));
		insuranceAdapter = new ArrayAdapter<InsuranceObject>(getActivity(), android.R.layout.simple_list_item_1, ((Global)getActivity().getApplication()).insurances);
		yearList = Arrays.asList(getResources().getStringArray(R.array.years));
		makeList = Arrays.asList(getResources().getStringArray(R.array.makes));
	}
	
//	@Override
//	public void onActivityCreated(Bundle savedInstanceState)
//	{
//		super.onActivityCreated(savedInstanceState);
//		updateModelSpinner();
//	}
	
	@Override
	public void onDestroy()
	{
		vehicle.name = name.getEditableText().toString();
		vehicle.tagNum = tagNum.getEditableText().toString();
		vehicle.vin = vinNum.getEditableText().toString();
		vehicle.color = color.getEditableText().toString();
		vehicle.registrationNum = registrationNum.getEditableText().toString();
		vehicle.year = Integer.parseInt((String)yearSpinner.getSelectedItem());
		vehicle.make = (String)makeSpinner.getSelectedItem();
		vehicle.model = (String)modelSpinner.getSelectedItem();
		super.onDestroy();
	}
	
	private void updateModelSpinner()
	{
		int modelIndex = 0;
		SpinnerAdapter modelAdapter = null;
		if(makeSpinner.getSelectedItem().equals("Dodge"))
		{
			modelAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, dodgeModels);
			modelIndex = dodgeModels.indexOf(vehicle.model);
		}
		else if(makeSpinner.getSelectedItem().equals("Ford"))
		{
			modelAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, fordModels);
			modelIndex = fordModels.indexOf(vehicle.model);
		}
		else if(makeSpinner.getSelectedItem().equals("Honda"))
		{
			modelAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, hondaModels);
			modelIndex = hondaModels.indexOf(vehicle.model);
		}
			
		modelSpinner.setAdapter(modelAdapter);
		if(modelIndex > -1)
			modelSpinner.setSelection(modelIndex);
	}

}
